package com.bocai.yuweiwangluo.activity;


import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.View.XListView;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.bean.MessageListBean;
import com.bocai.yuweiwangluo.presenter.MessageListPresenter;
import com.bocai.yuweiwangluo.viewModel.IMessageListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 消息中心界面
 */
public class MessageCenterActivity extends BaseActivity implements View.OnClickListener,XListView.IXListViewListener,IMessageListView,AdapterView.OnItemClickListener
{
    @ViewInject(R.id.lv_message_list)
    XListView lv_message_list;
    @ViewInject(R.id.ll_nocotent)
    LinearLayout llnoContent;
    @ViewInject(R.id.ll_refresh)
    LinearLayout llRefresh;
    @ViewInject(R.id.tv_setallmessage)
    TextView tv_setallmessage;
    private int page=1;
    private MessageListPresenter messageListPresenter;
    @Override
    protected int contentView()
    {
        return R.layout.activity_messagecenter;
    }

    @Override
    protected void setData()
    {
        ViewUtils.inject(this);
        initToolbar("消息中心");
        getMessageListPresenter();
        initEvent();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData()
    {

        messageListPresenter.getMessage(page + "");
    }

    @Override
    protected void initEvent()
    {
        mToolbar.inflateMenu(R.menu.menu_read);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                startActivity(new Intent(MessageCenterActivity.this, MessageYesCenterActivity.class));
                return true;
            }
        });
        llRefresh.setOnClickListener(this);
        lv_message_list.setPullLoadEnable(true);
        lv_message_list.setPullRefreshEnable(true);
        lv_message_list.setXListViewListener(this);
        tv_setallmessage.setOnClickListener(this);
        lv_message_list.setOnItemClickListener(this);
    }



    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ll_refresh:
                llRefresh.setVisibility(View.GONE);
                onRefresh();
                break;
            case R.id.tv_setallmessage:
                messageListPresenter.setAllMessage();
                break;
        }
    }

    @Override
    public void onRefresh()
    {
        page=1;
        lv_message_list.setPullLoadEnable(true);
        messageListPresenter.getMessage(page+"");
    }

    @Override
    public void onLoadMore()
    {
        page++;
        messageListPresenter.getMessage(page+"");
    }

    @Override
    public int getmyPage()
    {
        return page;
    }

    @Override
    public void setMyPage(int page) {
        this.page=page;
    }

    @Override
    public void onLoad()
    {
        lv_message_list.stopRefresh();
        lv_message_list.stopLoadMore();
        lv_message_list.setRefreshTime("");
    }

    @Override
    public LinearLayout getNoContent()
    {
        return llnoContent;
    }

    @Override
    public XListView getListView()
    {
        return lv_message_list;
    }

    @Override
    public MessageListPresenter getMessageListPresenter()
    {
        messageListPresenter=new MessageListPresenter(this,this,this);
        return messageListPresenter;
    }


    @Override
    public LinearLayout getllRefresh()
    {
        return llRefresh;
    }


    @Override
    public void finishActivity()
    {
        this.finish();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MessageListBean.DataEntity data= (MessageListBean.DataEntity) parent.getAdapter().getItem(position);
        Intent intent=new Intent(this,MessageDetailActivity.class);
        intent.putExtra("data",data);
        intent.putExtra("what","no");
        startActivity(intent);

    }
}
